<template>
  <div class="app-header">
    <div class="header-content">
      <div class="logo-container">
        <h1 class="logo-text">光伏知识图谱系统</h1>
      </div>
      <div class="user-actions">
        <div class="user-info" v-if="userInfo">
          <el-dropdown trigger="click" @command="handleCommand">
            <div class="user-dropdown-link">
              <el-avatar :size="32" class="user-avatar">
                {{ userInfo.realName?.charAt(0) || userInfo.username?.charAt(0) || 'U' }}
              </el-avatar>
              <span class="user-name">
                {{ userInfo.realName || userInfo.username }}
                <el-icon class="dropdown-icon"><arrow-down /></el-icon>
              </span>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="userInfo">
                  <el-icon><user /></el-icon>
                  <span>个人信息</span>
                </el-dropdown-item>
                <el-dropdown-item command="logout">
                  <el-icon><switch-button /></el-icon>
                  <span>退出登录</span>
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { computed } from 'vue';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';
import { ElMessageBox, ElMessage } from 'element-plus';
import { 
  ArrowDown, 
  User, 
  SwitchButton
} from '@element-plus/icons-vue';

export default {
  name: 'AppHeader',
  components: {
    ArrowDown,
    User,
    SwitchButton
  },
  setup() {
    const store = useStore();
    const router = useRouter();
    
    const userInfo = computed(() => {
      try {
        return store.getters.userInfo;
      } catch (error) {
        console.error('获取用户信息失败:', error);
        return null;
      }
    });
    
    const handleCommand = (command) => {
      if (command === 'userInfo') {
        // 跳转到个人信息页面
        ElMessage.info('功能开发中...');
      } else if (command === 'logout') {
        ElMessageBox.confirm('确定要退出登录吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          console.log('开始执行登出操作...');
          
          // 直接执行登出操作，无论成功与否都重定向到登录页
          store.dispatch('logout')
            .then(() => {
              console.log('登出成功，准备跳转到登录页');
              router.push('/login');
              ElMessage.success('退出登录成功');
            })
            .catch(() => {
              // 即使失败也重定向到登录页
              console.log('登出过程发生错误，但仍会重定向到登录页');
              router.push('/login');
              ElMessage.info('已退出系统');
            });
        }).catch(() => {
          console.log('用户取消了登出操作');
          // 取消操作
        });
      }
    };
    
    return {
      userInfo,
      handleCommand
    };
  }
};
</script>

<style scoped>
.app-header {
  height: 64px;
  background: linear-gradient(135deg, #1c3654 0%, #2c4a6c 100%);
  box-shadow: 0 2px 15px rgba(0, 0, 0, 0.15);
  position: relative;
  z-index: 100;
}

.header-content {
  position: relative;
  width: 100%;
  height: 100%;
}

.logo-container {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  display: flex;
  align-items: center;
}

.logo-text {
  margin: 0;
  padding-left: 30px;
  font-size: 22px;
  font-weight: 600;
  color: white;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.user-actions {
  position: absolute;
  right: 0;
  top: 0;
  height: 100%;
  display: flex;
  align-items: center;
  padding-right: 30px;
}

.user-dropdown-link {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 40px;
  transition: all 0.3s ease;
  background-color: rgba(255, 255, 255, 0.1);
}

.user-dropdown-link:hover {
  background-color: rgba(255, 255, 255, 0.2);
}

.user-avatar {
  background: linear-gradient(135deg, #4fc08d 0%, #3da776 100%);
  color: white;
  font-weight: bold;
  border: 2px solid rgba(255, 255, 255, 0.3);
}

.user-name {
  color: white;
  display: flex;
  align-items: center;
  font-size: 14px;
  gap: 5px;
}

.dropdown-icon {
  font-size: 12px;
  margin-top: 2px;
}

:deep(.el-dropdown-menu__item) {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
}

:deep(.el-dropdown-menu__item i) {
  font-size: 16px;
  color: var(--el-text-color-secondary);
}

@media (max-width: 768px) {
  .header-content {
    padding: 0 15px;
  }
  
  .user-name span {
    display: none;
  }
}
</style> 